package com.hzlj.position.dbsync.common.convert;

import cn.hutool.core.date.DateUtil;
import com.fz.common.base.enums.CoordinateType;
import com.hzlj.position.config.common.dto.common.PositionAddDTO;
import com.hzlj.position.config.common.dto.common.PositionAddExtDTO;
import com.hzlj.position.config.common.enums.Channel;
import com.hzlj.position.config.common.enums.ChannelType;
import com.hzlj.position.config.common.enums.ReportType;
import com.hzlj.position.config.common.enums.WarningType;
import com.hzlj.position.dbsync.common.config.PositionDbSyncConfig;

import java.math.BigDecimal;

import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

public class PositionAddConvert {
    public static PositionAddDTO convert(boolean checkExpire,
                                         WarningType warningType,
                                         String deviceCode,
                                         BigDecimal lon,
                                         BigDecimal lat,
                                         String time,
                                         Integer electricValue,
                                         String position,
                                         CoordinateType type) {
        String[] lonAndLat = type.toWGS84(lon.toPlainString(), lat.toPlainString());
        return convert(
                checkExpire,
                warningType,
                deviceCode,
                new BigDecimal(lonAndLat[0]),
                new BigDecimal(lonAndLat[1]),
                time,
                electricValue,
                position
        );
    }

    public static PositionAddDTO convert(boolean checkExpire,
                                         WarningType warningType,
                                         String deviceCode,
                                         BigDecimal lon,
                                         BigDecimal lat,
                                         String time,
                                         Integer electricValue,
                                         String position) {
        PositionAddDTO add = new PositionAddDTO();
        add.setId(POSITION_ID_GEN.nextIdStr());
        add.setChannel(Channel.WD.getCode());
        add.setChannelType(ChannelType.WD.getCode());
        add.setPositionTime(DateUtil.parse(time));
        add.setDeviceCode(deviceCode);
        add.setWarningType(warningType == null ? WarningType.SUCCESS.getCode() : warningType.getCode());
        add.setReportType(ReportType.WD.getCode());
        add.setLongitude(lon == null ? null : lon.toPlainString());
        add.setLatitude(lat == null ? null : lat.toPlainString());
        if (electricValue != null) {
            add.setPositionExt(
                    new PositionAddExtDTO().setElectricValue(electricValue)
            );
        }
        add.setPosition(position);
        //校验
        if (checkExpire) {
            PositionDbSyncConfig.checkExpire(time, add);
        }
        return add;
    }
}
